<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Modules_News_Models_Mysql_Articleauto extends Modules_News_Models_Articleauto
{	
	protected $_table = 'news_article_auto';
	
	public function getByLink($link)
	{
		$sql = 'SELECT * FROM ' . $this->_conn->_tablePrefix . $this->_table . ' 
							WHERE link_source = "' . $this->_conn->escape($link) . '" LIMIT 1';
		$rs = $this->_conn->query($sql);
		$return = (0 == mysql_num_rows($rs)) ? null : $this->_conn->fetchAll($rs);
		$this->_conn->freeResult($rs);
		return $return;
	}
	
	public function getArticleBySlug($slug)
	{
		$sql = 'SELECT * FROM ' . $this->_conn->_tablePrefix . $this->_table . ' 
							WHERE slug = "' . $this->_conn->escape($slug) . '" LIMIT 1';
		$rs = $this->_conn->query($sql);
		$return = (0 == mysql_num_rows($rs)) ? null : $this->_conn->fetchAll($rs);
		$this->_conn->freeResult($rs);
		return $return;
	}
	
	public function add($articleAuto)
	{
		return $this->_conn->insert($articleAuto, $this->_conn->_tablePrefix . $this->_table);	
	}
	
	public function find($limit)
	{
		$sql = 'SELECT a.title, a.* FROM ' . $this->_conn->_tablePrefix . $this->_table . ' AS a';
		$sql .= ' ORDER BY a.auto_id ASC';
		$sql .= ' LIMIT ' . $limit;
		$rs = $this->_conn->query($sql);
		$articles = array();
		while ($row = $this->_conn->fetchAll($rs)) {
			$articles[] = $row;
		}
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $articles;
	}
	
	public function delete($articleAutoId)
	{
		$sql = 'DELETE FROM ' . $this->_conn->_tablePrefix . $this->_table . ' 
							WHERE auto_id = ' . $this->_conn->escape($articleAutoId);
		$rs = $this->_conn->query($sql);
		$affectRow = $this->_conn->affectedRows();
		$this->_conn->freeResult($rs);
		return $affectRow;
	}
}